﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runda.CMS.Common.Utility;
using Runda.CMS.Dal;
using System.Data;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Bll
{
    public class MaintainBillBll:BllBase
    {
        public void UpdateAccessoryCheckStatus(Enumerator.AccessoryReplacementCheckStatus status, Guid MaintainID, Guid checkerID, string checkerName, string AccessoryReplaceMemo)
        {
            using (System.Transactions.TransactionScope tran = new System.Transactions.TransactionScope())
            {
                WsmMaintainBillDal dal = new WsmMaintainBillDal();

                WsmAccessoryReplacementDal adal = new WsmAccessoryReplacementDal();

                dal.Update(status, MaintainID, checkerID, checkerName, AccessoryReplaceMemo);
                adal.Update(status, MaintainID); 

                tran.Complete();
            }
        }
        
        public void UpdateMaintainBillCheckStatus(WsmAccessoryReplacementEntity entity,Guid CheckID,string CheckName)
        {
            using (System.Transactions.TransactionScope tran = new System.Transactions.TransactionScope())
            {
                WsmMaintainBillDal dal = new WsmMaintainBillDal();

                WsmAccessoryReplacementDal adal = new WsmAccessoryReplacementDal();

                dal.Update(Enumerator.AccessoryReplacementCheckStatus.Approved, entity.MaintainID,Guid.Empty,"","");
                adal.Update(entity);

                tran.Complete();
            }
        }

        public void UpdateMaintainBillCheckStatus(Guid MaintainID,Enumerator.AccessoryReplacementCheckStatus status, Guid UserID, string UserName)
        {
            WsmMaintainBillDal dal = new WsmMaintainBillDal();
            dal.Update(status, MaintainID, UserID, UserName,"");
        }

        public void UpdateMaintainBillCheckStatus(Guid MaintainID, Enumerator.InstrumentAssmblyStatus status, Guid UserID, string UserName)
        {
            WsmMaintainBillDal dal = new WsmMaintainBillDal();
            dal.Update(status, MaintainID, UserID, UserName);
        }

        public void CancelFinished(Guid registerID)
        {
            WsmMaintainBillDal dal = new WsmMaintainBillDal();
            dal.CancelUpdate(registerID);
        }

        public DataTable GetAccessortyTable(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            WsmAccessoryInfoDal dal = new WsmAccessoryInfoDal();
            return dal.GetAccessortyTable(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable GetCustomerInstrumentTable(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            CtrtCustomerDal dal = new CtrtCustomerDal();
            return dal.GetCustomerInstrumentList(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);
        }

        public DataTable ExportCustomerInstrumentTable(string WhereClause, string OrderBy)
        {
            CtrtCustomerDal dal = new CtrtCustomerDal();
            return dal.ExportCustomerInstrumentList(WhereClause, OrderBy);
        }

        public DataTable QueryMaintainBll(string WhereClause, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            WsmMaintainBillDal dal = new WsmMaintainBillDal();
            return dal.QueryMaintainBll(WhereClause, OrderBy, PageSize, PageIndex, out RecordCount);

        }
        //获得某仪器的配件信息
        public DataTable getComponentByInsurmentID(Guid insurmentID)
        {
            string sql = @"select b.ComponentName from Cms_Wsm_ComponentInstrument a
                                    inner join Cms_Wsm_Component b on a.ComponentID = b.ComponentID
                                    where a.InstrumentID='" + insurmentID.ToString() + "' order by ComponentDesc";
            return new CommonBll().getDataFromSQL(sql);
        }

        //获得客户仪器的汇总信息 客户数和仪器数
        public DataTable TotalCustomerInstrument(string WhereClause)
        {
            string sql = @"select COUNT(distinct CustomerID) as custs,COUNT(instrumentID) as instruments
            from CustomerInstrument
                                    where 1=1 " + WhereClause ;
            return new CommonBll().getDataFromSQL(sql);
        }

        //获得客户服务单的汇总信息 客户数和服务次数
        public DataTable TotalCustomerServices(string WhereClause)
        {
            string sql = @"select COUNT(distinct CustomerID) as custs,COUNT(distinct MaintainID) as serviceCount from (select c.AccessoryID,A.*,case when checkerid is null then 0 else 1 end as checkStatus ,B.CustomerID,B.TaskType,B.ExpectEndDate,B.InstrumentID,B.IsSystemAssign
From Cms_Wsm_MaintainBill A 
join Cms_Wsm_TaskRegister B on A.RegisterID = B.RegisterID
left join
(
    select MaintainID,count(*) as AccessoryID from Cms_Wsm_AccessoryReplacement group by MaintainID
) c on c.MaintainID = a.MaintainID) as TT
where  1=1 and Deleted = 0 " + WhereClause;
            return new CommonBll().getDataFromSQL(sql);
        }

        //获得客户更换配件 客户数、配件数和报价总金额
        public DataTable TotalAccessoryReplace(string WhereClause)
        {
            string sql = @"select COUNT(distinct CustomerID) as custs,sum(QuotePrice) as QuotePrice,count(*) as pjquantity
            from 
(
select A.*,B.CompanyInstrumentCode,B.AccessoryType,C.PaperInvoicesNo,c.RegisterID,CustomerID
from Cms_Wsm_AccessoryReplacement  A 
inner JOIN Cms_Wsm_AccessoryInfo B ON A.AccessoryID = B.AccessoryID
inner join Cms_Wsm_MaintainBill C on A.MaintainID = C.MaintainID
inner join Cms_Wsm_TaskRegister d on d.RegisterID = c.RegisterID
) z
                                    where 1=1 " + WhereClause;
            return new CommonBll().getDataFromSQL(sql);
        }


        //获得某仪器的最新的软件版本
        public string getInstrumentSoftwareversion(string registerID)
        {
            string softversion = "";
            string sql = @"select SoftWareVersion from Cms_Wsm_InstrumentAssembly where MaintainID in
                            (
	                            select MaintainID from Cms_Wsm_MaintainBill where RegisterID in
	                            (
		                            select RegisterID from Cms_Wsm_TaskRegister where RegisterID ='" + registerID + "'))";

            return new CommonBll().getDataFromSQL(sql).Rows[0][0].ToString();
        }

        //判断保内订单入库的关联的服务单号
        public int IsExistsMaintainBill(string invNO)
        {
            return new WsmMaintainBillDal().IsExistsMaintainBill(invNO);
        }

        //更新保内配件入库状态
        public void updateAccessoryReplacement(string billNo,string accessoryID)
        {
            new WsmMaintainBillDal().updateAccessoryReplacement(billNo, accessoryID);
        }

        //判断序列号是否存在
        public int isExistSerialNum(string AssemblyID, string SerialNum)
        {
            string sql = "select count(*) from Cms_Wsm_InstrumentAssembly where SerialNum='" + SerialNum + "' and Deleted=0 and AssemblyID<>'" + AssemblyID+"'";
            return System.Convert.ToInt16(new CommonBll().getDataFromSQL(sql).Rows[0][0].ToString());
        }
        //导出服务单
        public DataTable ExportMaintainBills(string WhereClause)
        {
            string sql = @"select 服务单号,配件更换,客户名称,所在区域,现场工程师,工作类型,工作内容,到达时间,结束时间,仪器名称,仪器序列号,故障代码,故障组件,服务目的,服务后仪器状态
            from vw_maintainbill_export tt
                                    where 1=1 " + WhereClause;
            return new CommonBll().getDataFromSQL(sql);
        }

    }
}
